package com.cn.bukeManage.service.serviceImpl;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cn.bukeManage.dao.TaskManageDao;
import com.cn.bukeManage.pojo.Customer_address;
import com.cn.bukeManage.pojo.Indent;
import com.cn.bukeManage.pojo.Territory;
import com.cn.bukeManage.service.TaskManageService;


@Service("taskManageService")
public class TaskManageServiceImpl implements TaskManageService {
	@Resource
	private TaskManageDao taskManageDao;

	//下维修订单
	public Integer toAddRepairIndent(String name, String phone, String subscribeTime, String weiXbody, Integer province,
			Integer city, Integer village, String address,Double indentPrice) {
		//把地址存到customer_address,返回一个id
		String sheng=taskManageDao.getAreaNameByCode(province);
		String shi=taskManageDao.getAreaNameByCode(city);
		String xian=taskManageDao.getAreaNameByCode(village);
		String pincity=sheng+shi+xian;
		Customer_address customer_address=new Customer_address();
		customer_address.setCity(pincity);
		customer_address.setAddress(address);
		customer_address.setName(name);
		customer_address.setPhone(phone);
		taskManageDao.addNewCustomer_address(customer_address);
		//再把id存到indent中 type=2 weiXbody indentType=2 beginTime subscribeTime
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Indent indent=new Indent();
		indent.setType(2);
		indent.setWeiXbody(weiXbody);
		indent.setBeginTime(new Date());
		indent.setIndentPrice(indentPrice);
		try {
			indent.setSubscribeTime(simpleDateFormat.parse(subscribeTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		indent.setIndentType(2);
		indent.setCuAdId(customer_address.getCuAdId());
		Integer aInteger =taskManageDao.addNewIndentOfRepair(indent);	
		return aInteger;
	}

	//下测量订单
	public Integer toAddMeasureIndent(String name, String phone, String subscribeTime, Integer roomNumB,
			Integer province, Integer city, Integer village, String address,Double addPrice,Double indentPrice) {
		String sheng=taskManageDao.getAreaNameByCode(province);
		String shi=taskManageDao.getAreaNameByCode(city);
		String xian=taskManageDao.getAreaNameByCode(village);
		String pincity=sheng+shi+xian;
		Customer_address customer_address=new Customer_address();
		customer_address.setCity(pincity);
		customer_address.setAddress(address);
		customer_address.setName(name);
		customer_address.setPhone(phone);
		taskManageDao.addNewCustomer_address(customer_address);		
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Indent indent=new Indent();
		indent.setType(0);
		indent.setRoomNumB(roomNumB);
		indent.setBeginTime(new Date());
		try {
			indent.setSubscribeTime(simpleDateFormat.parse(subscribeTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		indent.setIndentType(2);
		indent.setStatus(0);
		indent.setCuAdId(customer_address.getCuAdId());
		indent.setAddPrice(addPrice);
		indent.setIndentPrice(indentPrice);
		Integer aInteger =taskManageDao.addNewIndentOfMeasure(indent);			
		return aInteger;
	}

	//返回测量费的计算方法
	public Territory getTerritoryByCode(Integer province, Integer city) {
		String Province=taskManageDao.getAreaNameByCode(province);
		String City=taskManageDao.getAreaNameByCode(city);
		Map<String,Object> map=new HashMap<>();
		map.put("province",Province);
		map.put("city",City);
		Territory territory=taskManageDao.getTerritoryByProAndCity(map);
		return territory;
	}

}
